package com.ocom.aiot.controller;

import com.github.pagehelper.PageInfo;
import com.ocom.aiot.service.UserInfoService;
import com.ocom.aiot.vo.GetUserListVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.entity.aiot.EntityUserInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
public class UserInfoController {

    private final Logger log = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;

    // 2.3.1.获取用户信息（getUserInfo）
    @GetMapping(AiotUrl.URL_Aiot_GetUserInfo)
    public Result GetUserInfo(String account){
        EntityUserInfo entityUserInfo = null;
        try {
            log.info("查找账号"+account);
            entityUserInfo = userInfoService.GetUserInfo(account);
            if (entityUserInfo == null){
                log.error("没有找到用户"+entityUserInfo);
                return ResultUtil.error(ResultEnum.Err_1001,entityUserInfo);
            }
            log.info("账户数据"+entityUserInfo);
        }catch (Exception  exception){
            log.error(exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }

        return ResultUtil.success(entityUserInfo);
    }

    // 2.3.2.更新用户信息（updateUserInfo）
    @PostMapping(AiotUrl.URL_Aiot_UpdateUserInfo)
    public Result UpdateUserInfo(@Validated @RequestBody EntityUserInfo userInfo){
        if (userInfo.getUserId() < 1 || userInfo.getSaasNo().isEmpty()){
            log.error("用户Id小于1"+userInfo.getUserId(),"saas平台绑定编号为空"+userInfo.getSaasNo());
            return ResultUtil.error(ResultEnum.Err_203,userInfo.getSaasNo()+" "+userInfo.getUserId());
        }
        try {
            userInfoService.UpdateUserInfo(userInfo);
        }catch (Exception exception){
            log.error(exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }
        return ResultUtil.success();
    }

    // 2.3.3.添加用户信息（addUserInfo）
    @PostMapping(AiotUrl.URL_Aiot_AddUserInfo)
    public Result AddUserInfo(@Validated @RequestBody EntityUserInfo entityUserInfo){
        if (entityUserInfo.getAccount().isEmpty() || entityUserInfo.getSaasNo().isEmpty() ||
                entityUserInfo.getUserName().isEmpty() || entityUserInfo.getPassword().isEmpty()){
            log.error("登录账户不允许为空"+entityUserInfo.getUserId(),"saas平台绑定编号为空"+entityUserInfo.getSaasNo());
            log.error("用户姓名不允许为空"+entityUserInfo.getUserName(),"密码不允许为空"+entityUserInfo.getPassword());
            return ResultUtil.error(ResultEnum.Err_203,entityUserInfo);
        }
        try {
            userInfoService.AddUserInfo(entityUserInfo);
        }catch (Exception exception){
            log.error(exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }
        return ResultUtil.success();
    }

    // 获取用户列表（getUserList）
    @PostMapping(AiotUrl.Url_Aiot_GetUserList)
    public Result GetUserList(@Validated @RequestBody GetUserListVo getUserListVo){
       Map<String, Object> map = new HashMap<>();
        try {
            List<EntityUserInfo> entityUserInfos = userInfoService.GetUserList(getUserListVo);
            PageInfo<EntityUserInfo> entityUserInfoPageInfo = new PageInfo<>(entityUserInfos);
            map.put("total",entityUserInfoPageInfo.getTotal());
            map.put("list",entityUserInfos);
        }catch (Exception exception){
            log.error(exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"查询用户列表失败");
        }
        return ResultUtil.success(map);
    }
}
